unit BDEQueryTests;

interface

uses
  DBTables,
  TestFrameWork;

type
  TBDEQueryTests = class(TTestCase)
  private
    FDatabase: TDatabase;
    FQuery: TQuery;

  protected

    procedure SetUp; override;
    procedure TearDown; override;

  published

    // Test methods
    procedure TestCreate;
    procedure TestDestroy;
    procedure TestPrepare;
    procedure TestUnprepare;
    procedure TestGetCurrentRecord;
    procedure TestGetFieldData;
    procedure TestGetBlobFieldData;
    procedure TestCreateBlobStream;
//    procedure TestFetchAll;
//    procedure TestApplyUpdates;
//    procedure TestCancelUpdates;
//    procedure TestRevertRecord;
//    procedure TestIsSequenced;
//    procedure TestUpdateStatus;
//    procedure TestBookmarkValid;
//    procedure TestCompareBookmarks;
//    procedure TestLocate;
//    procedure TestLocateNext;
//    procedure TestLookup;
//    procedure TestPrepareStatement;
//    procedure TestParseMacroSQL;

  end;

implementation

uses SysUtils, DB;

{ TBDEQueryTests }

const
  INTERBASE_PARAMS =
    'SERVER NAME=D:\data\luxena\Products\dbExpress_Extension\Test\TestData\EMPLOYEE.GDB' + SLineBreak +
    'USER NAME=sysdba' + SLineBreak +
    'PASSWORD=masterkey' + SLineBreak +
    'OPEN MODE=READ/WRITE' + SLineBreak +
    'SCHEMA CACHE SIZE=8' + SLineBreak +
    'LANGDRIVER=' + SLineBreak +
    'SQLQRYMODE=' + SLineBreak +
    'SQLPASSTHRU MODE=SHARED AUTOCOMMIT' + SLineBreak +
    'SCHEMA CACHE TIME=-1' + SLineBreak +
    'MAX ROWS=-1' + SLineBreak +
    'BATCH COUNT=200' + SLineBreak +
    'ENABLE SCHEMA CACHE=FALSE' + SLineBreak +
    'SCHEMA CACHE DIR=' + SLineBreak +
    'ENABLE BCD=FALSE' + SLineBreak +
    'BLOBS TO CACHE=64' + SLineBreak +
    'BLOB SIZE=32' + SLineBreak +
    'WAIT ON LOCKS=FALSE' + SLineBreak +
    'COMMIT RETAIN=FALSE' + SLineBreak +
    'ROLE NAME=';
    
procedure TBDEQueryTests.SetUp;
begin
  inherited;
  FDatabase := TDatabase.Create(nil);
  FDatabase.DriverName := 'INTRBASE';
  FDatabase.Params.Text := INTERBASE_PARAMS;
  FDatabase.DatabaseName := 'testdb';
  FDatabase.LoginPrompt := False;
  FDatabase.Open;
  FQuery := TQuery.Create(nil);
  FQuery.DatabaseName := FDatabase.DatabaseName;
  FQuery.SQL.Text := 'select * from customer';
end;

procedure TBDEQueryTests.TearDown;
begin
  inherited;
  FreeAndNil(FDatabase);
  FreeAndNil(FQuery);
end;

procedure TBDEQueryTests.TestCreate;
begin
  Assert(FQuery <> nil);
end;

procedure TBDEQueryTests.TestDestroy;
begin
  FreeAndNil(FQuery);
end;

procedure TBDEQueryTests.TestPrepare;
begin
  FQuery.Prepare;
end;

procedure TBDEQueryTests.TestUnprepare;
begin
  FQuery.Unprepare;
  FQuery.Prepare;
  FQuery.Unprepare;
end;

procedure TBDEQueryTests.TestGetCurrentRecord;
begin
  Assert(not FQuery.GetCurrentRecord(nil));
  FQuery.Open;
  Assert(FQuery.GetCurrentRecord(nil));
end;

procedure TBDEQueryTests.TestGetFieldData;
var
  Buffer: array of Byte;
begin
  FQuery.Open;
  SetLength(Buffer, FQuery.Fields[0].DataSize);
  FQuery.Close;
  FQuery.SQL.Text := 'select * from customer where 1=0';
  FQuery.Open;
  Assert(not FQuery.GetFieldData(FQuery.Fields[0], Pointer(Buffer)));
end;

procedure TBDEQueryTests.TestGetBlobFieldData;
var
  Buffer: TBlobByteData;
begin
  FQuery.Open;
  try
    FQuery.GetBlobFieldData(1, Buffer);
    Fail('Cannot get blob data for a non blob field');
  except
  end;
  FQuery.Close;
  FQuery.SQL.Text := 'select * from job';
  FQuery.Open;
  Assert(FQuery.GetBlobFieldData(
    FQuery.FieldByName('JOB_REQUIREMENT').FieldNo, Buffer) > 0);
  FQuery.Close;
  FQuery.SQL.Text := 'select * from job where 1=0';
  FQuery.Open;
  Assert(FQuery.GetBlobFieldData(
    FQuery.FieldByName('JOB_REQUIREMENT').FieldNo, Buffer) = 0);
end;

procedure TBDEQueryTests.TestCreateBlobStream;
begin

end;

initialization

  TestFramework.RegisterTest('BDEQueryTests Suite', TBDEQueryTests.Suite);

end.
